﻿using System.Drawing;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using PlanningGIS.ArcGIS;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 主体功能区图例窗体
    /// </summary>
    public partial class FrmLegend : Office2007Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmLegend()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗体实例
        /// </summary>
        private static FrmLegend mInstance;

        /// <summary>
        /// 创建实例
        /// </summary>
        /// <returns></returns>
        public static FrmLegend Instance()
        {
            if (mInstance != null)
                mInstance.Dispose();
            mInstance = new FrmLegend();
            return mInstance;
        }
        /// <summary>
        /// 更新图例
        /// </summary>
        /// <param name="pMap">地图对象</param>
        /// <param name="layers">图层名</param>
        public void ReLoad(IMap pMap, string layers)
        {
            ESRI.ArcGIS.Controls.AxSymbologyControl pSc = new AxSymbologyControl();
            ((System.ComponentModel.ISupportInitialize)(pSc)).BeginInit();
            this.Controls.Add(pSc);
            ((System.ComponentModel.ISupportInitialize)(pSc)).EndInit();

            string[] sLayer = layers.Split(";".ToCharArray());
            for (int k = 0; k < sLayer.Length; k++)
            {
                string s = sLayer[k];
                string layerName = s;
                //string labelName = "";
                string labelText = "";
                if (s.Contains(":"))
                {
                    layerName = s.Substring(0, s.IndexOf(":"));
                    //labelName = s.Substring(s.IndexOf(":") + 1);
                }
                ILayer pLayer = SPGetLayer(pMap, layerName);
                if (pLayer == null) continue;
                labelText = layerName.Substring(s.IndexOf("/") + 1);
                ISymbologyStyleClass symbologyStyleClass = pSc.GetStyleClass(GetStyleType(((IFeatureLayer)pLayer).FeatureClass.ShapeType));
                IGeoFeatureLayer pGeoLayer = (IGeoFeatureLayer)pLayer;
                ButtonItem item = null;
                if (pGeoLayer.Renderer is ISimpleRenderer)
                {
                    ISimpleRenderer pSimpleRenderer = (ISimpleRenderer)pGeoLayer.Renderer;
                    item = new ButtonItem();
                    item.ButtonStyle = eButtonStyle.ImageAndText;
                    item.Image = GetPreviewImage(50, 25, pSimpleRenderer.Symbol, symbologyStyleClass);
                    //item.Text = labelName;
                    item.Text = labelText;
                    itemLegend.Items.Add(item);
                }
                else if (pGeoLayer.Renderer is IUniqueValueRenderer)
                {
                    IUniqueValueRenderer pUniqueValueRenderer = (IUniqueValueRenderer)pGeoLayer.Renderer;
                    for (int i = 0; i < pUniqueValueRenderer.ValueCount; i++)
                    {
                        item = new ButtonItem();
                        item.ButtonStyle = eButtonStyle.ImageAndText;
                        item.Text = pUniqueValueRenderer.get_Label(pUniqueValueRenderer.get_Value(i));
                        item.Image = GetPreviewImage(50, 25, pUniqueValueRenderer.get_Symbol(pUniqueValueRenderer.get_Value(i)), symbologyStyleClass);
                        itemLegend.Items.Add(item);
                    }
                }
                else if (pGeoLayer.Renderer is IClassBreaksRenderer)
                {
                    IClassBreaksRenderer pClassBreaksRenderer = (IClassBreaksRenderer)pGeoLayer.Renderer;
                    for (int i = 0; i < pClassBreaksRenderer.BreakCount; i++)
                    {
                        item = new ButtonItem();
                        item.ButtonStyle = eButtonStyle.ImageAndText;
                        item.Text = pClassBreaksRenderer.get_Label(i);
                        item.Image = GetPreviewImage(50, 25, pClassBreaksRenderer.get_Symbol(i), symbologyStyleClass);
                        itemLegend.Items.Add(item);
                    }
                }
            }
        }
        /// <summary>
        /// 获取图层
        /// </summary>
        /// <param name="pMap">地图对象</param>
        /// <param name="sLayer">图层路径</param>
        /// <returns></returns>
        public static ILayer SPGetLayer(IMap pMap, string sLayer)
        {
            string[] s = sLayer.Split("/".ToCharArray());
            int i;
            int j = 0;
            ILayer pLayer;
            ICompositeLayer pComppLayer = null;
            for (i = 1; i < s.Length; i++)
            {
                if (i == 1)
                {
                    pLayer = LayerHelper.GetLayer(pMap, s[i - 1], ref j);
                }
                else
                {
                    pLayer = LayerHelper.GetLayer(pComppLayer, s[i - 1], 0);
                }
                pComppLayer = (ICompositeLayer)pLayer;
            }
            if (s.Length == 1)
            {
                pLayer = LayerHelper.GetLayer(pMap, s[s.Length - 1], ref j);
            }
            else
            {
                pLayer = LayerHelper.GetLayer(pComppLayer, s[s.Length - 1], 0);
            }
            return pLayer;
        }

        /// <summary>
        /// 图例item预览图片
        /// </summary>
        /// <param name="Width">宽度</param>
        /// <param name="Height">高度</param>
        /// <param name="pSymbol">Symbol对象</param>
        /// <param name="symbologyStyleClass">符号化类型</param>
        /// <returns></returns>
        private Image GetPreviewImage(int Width, int Height, ISymbol pSymbol, ISymbologyStyleClass symbologyStyleClass)
        {
            IStyleGalleryItem styleGalleryItem = new ServerStyleGalleryItem();
            styleGalleryItem.Item = pSymbol;

            //Preview an image of the symbol
            stdole.IPictureDisp picture = symbologyStyleClass.PreviewItem(styleGalleryItem, Width, Height);
            System.Drawing.Image image = System.Drawing.Image.FromHbitmap(new System.IntPtr(picture.Handle));
            return image;
        }
        /// <summary>
        /// 符号化类型
        /// </summary>
        /// <param name="iGeoType"></param>
        /// <returns>图层几何类型（点、线、面）</returns>
        private esriSymbologyStyleClass GetStyleType(esriGeometryType iGeoType)
        {
            esriSymbologyStyleClass iStyleType = esriSymbologyStyleClass.esriStyleClassMarkerSymbols;
            switch (iGeoType)
            {
                case esriGeometryType.esriGeometryPoint:
                    iStyleType = esriSymbologyStyleClass.esriStyleClassMarkerSymbols;
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    iStyleType = esriSymbologyStyleClass.esriStyleClassLineSymbols;
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    iStyleType = esriSymbologyStyleClass.esriStyleClassFillSymbols;
                    break;
            }
            return iStyleType;
        }
    }
}
